/////////////////////////////////////////////////////////////////////////
//	AUTHOR: 		Eric IJzer                                         //
//	FILE:			TCPSocket.cpp                         		       //
//	DESCRIPTION:	Implementation file of TCPSocket class			   //
/////////////////////////////////////////////////////////////////////////

#include "TCPSocket.h"

/////////////////////////////
//		CONSTRUCTORS       //
/////////////////////////////

/*
 * Create new TCPSocket
 *
 * portNumber: 	PortNumber to use
 * queueLength:	Maximum length of the queue
 */
TCPSocket::TCPSocket(int portNumber, int queueLength) {
	// Create socket
	this->sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

	// Failed to create socket
	if(sock < 0) {
		cout << "Unable to create socket" << endl;
	}
	// Socket succesfully created
	else {
		// TODO Bind, Listen
	}
}


/*
 * Create new TCPSocket
 *
 * sock:		Socket that has to be used
 */
TCPSocket::TCPSocket(int sock) {
	// Set socket
	this->sock = sock;
}




/////////////////////////////
//    PUBLIC FUNCTIONS     //
/////////////////////////////

/*
 * Accept new connections
 *
 * 	Returns: 	TCPSocket for the accepted client
 */
TCPSocket* TCPSocket::Accept() {
    // accept new connection
    int newSock = ::accept(sock, NULL, 0);

    // failed to accept
    if (newSock < 0) {
    	cout << "Unable to accept a new connection" << endl;
    }

    // Retun TCPsocket van de verbonden client
    return new TCPSocket(newSock);
}



/////////////////////////////
//    PRIVATE FUNCTIONS    //
/////////////////////////////



















/*
 * Deconstructor
 */
TCPSocket::~TCPSocket() {
}

